package ualrcdg.client;

import ualrcdg.shared.dto.Game;
import ualrcdg.shared.dto.MediaScript;
import ualrcdg.shared.dto.MediaScriptEntry;
import ualrcdg.shared.dto.Page;


/**
 * This class renders Scripts to deliver content
 * based on the completed tasks.
 */
public class ScriptRenderer
{
	private String scriptId;
	private static String currentState = "";
	//TODO when GameEngine is functional, remove ScriptRenderer references to Game game; replace with references to GameEngine's game object
	private Game game = null;
	private boolean onInitialState;

	/**
	 * This is a nullary constructor
	 */

	public ScriptRenderer( Game g )
	{
		this.game = g;
		onInitialState = true;
	}

	/**
	 * This renders scripts according to the game and script Ids.
	 * @param buttonNo the button number that was pressed
	 */

	public String renderScript( String buttonNo )
	{
		MediaScript ms = game.getMediaScript( scriptId );
		if( onInitialState )
		{
			currentState = ms.getInitialState( );
			System.out.println( currentState );
			String currentPageId = ms.getPageId( currentState );
			Page currentPage = game.getPage( currentPageId );
			onInitialState = false;

			return currentPage.getLayoutId( );
		}

		MediaScriptEntry mse = ms.getTransition( currentState, buttonNo );
		currentState = mse.getDestinationState( );
		System.out.println( currentState );
		String currentPageId = ms.getPageId( currentState );
		Page currentPage = game.getPage( currentPageId );

		/* 
		String audioIds[] = Arrays.copyOf(currentPage.getAudioId(), currentPage.getAudioId().length);
		String hapticIds[] = Arrays.copyOf(currentPage.getHapticId(), currentPage.getHapticId().length);
		String updateIds[] = Arrays.copyOf(initialPage.getUpdateId(), initialPage.getUpdateId().length); 
		*/

		return currentPage.getLayoutId( );
	}

	//TODO Comment method

	public String nextState( String buttonNo )
	{
		if( currentState == null )
		{
			return "";
		}
		MediaScript ms = game.getMediaScript( scriptId );
		MediaScriptEntry mse = ms.getTransition( currentState, buttonNo );

		return mse.getDestinationState( );
	}

	/**
	 * This function sets the scriptId
	 * @param scriptId to be assigned to a script
	 */
	public void setScriptId( String scriptId )
	{
		this.scriptId = scriptId;
	}
}
